<template>
  <div class="GameDetail">
    <!-- 顶部标题 -->
    <top-title-bar>
      <template #left>
        <div class="left" @click="$router.go(-1)" ref="header">
          <span class="iconfont icon-xiangzuo1"></span>
        </div>
      </template>
      <template #right>
        <div class="right" @click="showShare = true">
          <span class="iconfont icon-gengduo-copy"></span>
        </div>
      </template>
    </top-title-bar>

    <!-- 版心 -->
    <div class="wrap" v-if="GameDetail">
      <nav>
        <div
          class="banner-img"
          @click="ImagePreviewFun([GameDetail.gameinfo.coverImage], 0)"
        >
          <img :src="GameDetail.gameinfo.coverImage" alt="" />
        </div>

        <div class="game-imfo">
          <!-- - 左边 -->
          <div class="left">
            <div
              class="icon"
              @click="ImagePreviewFun([GameDetail.gameinfo.applogo], 0)"
            >
              <img :src="GameDetail.gameinfo.applogo" alt="" />
            </div>
          </div>

          <!-- - 右边 -->
          <div class="right">
            <h4>{{ GameDetail.gameinfo.apptitle }}</h4>

            <p>
              版本{{ GameDetail.gameinfo.appversion }}
              {{ GameDetail.gameinfo.appsize }}M
            </p>

            <div class="desc">
              {{ GameDetail.gameinfo.apptags.split(",").join(" | ") }}
            </div>

            <div class="badge"></div>
          </div>
        </div>

        <!-- <div class="nav">
          <p class="con"><span>详情</span></p>
          <p><span>专区</span></p>
          <p><span>礼包</span></p>
          <p><span>评论</span></p>
        </div> -->
      </nav>

      <div class="game-introduce">
        <!-- - 头部 -->
        <div class="head">
          <h4>应用介绍</h4>

          <div class="tags">
            <p v-for="tag in GameDetail.gameinfo.appTypes" :key="tag.type_id">
              #<span>{{ tag.type_name }}</span>
            </p>
          </div>
        </div>

        <!-- - 身体 -->
        <div class="body">
          <div class="banner">
            <div
              class="img"
              v-for="(img, i) in GameDetail.gameinfo.imageresource"
              :key="img"
              @click="ImagePreviewFun(GameDetail.gameinfo.imageresource, i)"
            >
              <img :src="img" alt="" />
            </div>
          </div>

          <div class="text">
            <p class="text-ellipsis-3">
              {{ GameDetail.gameinfo.appdesc }}
            </p>
          </div>
        </div>

        <!-- - 尾部 -->
        <div class="foot">
          <div class="toggle"></div>
        </div>
      </div>

      <div class="game-introduce update">
        <!-- - 头部 -->
        <div class="head">
          <h4>
            更新说明
            <a
              href=""
              target="_blank"
              class="history"
              @click.prevent="$toast('有且仅有一个版本')"
              >更新历史
            </a>
          </h4>
          <p>
            版本{{ GameDetail.gameinfo.appversion }}
            {{ GameDetail.gameinfo.updateTime | YMDtime }}
          </p>
        </div>

        <!-- - 身体 -->
        <div class="body">
          <div class="text">
            <p class="text-ellipsis-3">无</p>
          </div>
        </div>

        <!-- - 尾部 -->
        <div class="foot">
          <div class="toggle"></div>
        </div>

        <div class="detailed">
          <h4>详细信息</h4>

          <div class="table">
            <div class="thead">
              {{ GameDetail.gameinfo.apptags.split(",").join(" | ") }}
            </div>

            <div class="tbody">
              <p>
                <span class="key"
                  >版本号 <em @click="$toast('已提交催更申请')">催更</em></span
                >
                <span class="val">{{ GameDetail.gameinfo.appversion }}</span>
              </p>
              <p>
                <span class="key">更新时间 </span>
                <span class="val">{{
                  GameDetail.gameinfo.updateTime | YMDtime
                }}</span>
              </p>
              <p>
                <span class="key">作者 </span>
                <span class="val">{{ GameDetail.gameinfo.username }}</span>
              </p>
              <p>
                <span class="key">应用大小 </span>
                <span class="val">{{ GameDetail.gameinfo.appsize }}mb</span>
              </p>
              <p>
                <span class="key">系统要求 </span>
                <span class="val">{{ GameDetail.gameinfo.system }}</span>
              </p>
            </div>
          </div>
        </div>
      </div>

      <div class="recommended">
        <div class="head">
          <h4>猜你喜欢</h4>
        </div>

        <div class="body" v-if="GameDetail.similarList.length > 8">
          <div
            class="game-item"
            v-for="index in 8"
            :key="index"
            @click="$toast('数据不够 你已经在本页')"
          >
            <!-- - 图片 -->
            <div class="icon">
              <img :src="GameDetail.similarList[index].applogo" alt="" />
            </div>

            <div class="text">
              <span class="text-ellipsis-1">
                {{ GameDetail.similarList[index].apptitle }}
              </span>
            </div>
          </div>
        </div>
        <div class="body" v-if="GameDetail.similarList.length < 8">
          <div
            class="game-item"
            v-for="similar in GameDetail.similarList"
            :key="similar.appid"
            @click="$toast('数据不够 你已经在本页')"
          >
            <!-- - 图片 -->
            <div class="icon"><img :src="similar.applogo" alt="" /></div>

            <div class="text">
              <span class="text-ellipsis-1">
                {{ similar.apptitle }}
              </span>
            </div>
          </div>
        </div>
      </div>

      <div class="ipone-card">
        <p>免流量特权 <span>（免流量畅玩葫芦侠）</span></p>
      </div>

      <div class="download">
        <a :href="GameDetail.gameinfo.dataDownUrl" target="_blank"
          >下载（384.16MB）</a
        >
      </div>
    </div>

    <van-share-sheet
      v-model="showShare"
      title="立即分享给好友"
      :options="options"
      @select="onSelect"
    />
  </div>
</template>


<script>
import TopTitleBar from "../components/TopTitleBar.vue";
import { getHomeJson } from "../assets/api/home.js";
import { ImagePreview } from "vant";
export default {
  data() {
    return {
      GameDetail: null,
      header: null,

      // 分享面板配置
      showShare: false,
      options: [
        { name: "微信", icon: "wechat" },
        { name: "微博", icon: "weibo" },
        { name: "复制链接", icon: "link" },
        { name: "分享海报", icon: "poster" },
        { name: "二维码", icon: "qrcode" },
      ],
    };
  },
  methods: {
    // 数据初始化
    dataInit() {
      this.GameDetailInit();
      window.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    },

    // 图片预览
    ImagePreviewFun(images, startPosition) {
      ImagePreview({ images, startPosition });
    },

    // 分享面板
    onSelect(option) {
      this.$toast(option.name);
      this.showShare = false;
    },

    // 游戏数据初始化
    async GameDetailInit() {
      let data = await getHomeJson();
      let app_id = this.$route.query.app_id;
      if (!app_id) console.log("没有传参");

      app_id = 86359;
      console.log("数据不够 已经将app_id换成已有的 =>", app_id);
      console.log(data.GameDetailList);
      this.GameDetail = data.GameDetailList.find(
        (item) => item.gameinfo.appid == app_id
      );
      console.log("取到的数据", this.GameDetail);

      this.__scrollEvent();
    },

    // 滚动条事件
    __scrollEvent() {
      this.header = this.$refs.header.parentElement;
      window.onscroll = () => {
        if (160 - window.scrollY < 0) {
          console.log("变深");
          //   计算差值
          let dScrollY = Math.abs(160 - window.scrollY);
          this.header.style.backgroundColor =
            "rgba(19, 207, 99, " + dScrollY / 50 + ")";
        }
        if (window.scrollY < 15)
          this.header.style.backgroundColor = "rgba(19, 207, 99, " + 0 + ")";
      };
    },
  },
  created() {
    this.dataInit();
  },
  beforeDestroy() {
    // 清除滚动事件
    console.log("beforeDestroy 清除滚动事件");
    window.onscroll = null;
  },
  components: {
    TopTitleBar,
  },
  filters: {
    YMDtime(val) {
      let date = new Date(val * 1000);
      let y = date.getFullYear();
      let m = date.getMonth();
      let d = date.getDate() + 1;
      return `${y}-${m}-${d}`;
    },
  },
};
</script>

<style lang="scss" scoped>
.GameDetail {
  min-height: 100%;
  background-color: #fff;
  ::v-deep .TopTitleBar {
    .height {
      height: 0;
    }
    header {
      background: none;
    }
    .left {
      display: flex;
      align-items: center;
      .icon-xiangzuo1 {
        font-weight: bold;
        color: #fff;
        font-size: 22px;
      }
    }
    .right {
      .icon-gengduo-copy {
        // font-weight: bold;
        color: #fff;
        font-size: 22px;
        &.icon-xiaoxi {
          margin-left: 18px;
        }
      }
    }
  }

  .wrap {
    nav {
      display: block;
      position: relative;
      z-index: 5;
      top: 0;
      height: max-content;

      .banner-img {
        margin-bottom: 12px;
        height: 210px;
      }

      .game-imfo {
        display: flex;
        padding: 0 12px;

        .icon {
          width: 61px;
          height: 61px;
          border-radius: 12px;
          overflow: hidden;
          margin-right: 10px;
        }

        .right {
          flex: 1;
          position: relative;

          .badge {
            position: absolute;
            // background: url("../imgs/good.png") no-repeat 0 0;
            background-size: 63px 50px;
            width: 63px;
            height: 50px;
            top: 50%;
            transform: translateY(-50%);
            right: 10px;
          }
        }

        h4 {
          font-size: 15px;
          color: #404040;
          margin-top: 2px;
          height: 22px;
          line-height: 22px;
        }

        p {
          font-size: 12px;
          color: #9d9d9d;
          text-align: left;
          height: 19px;
          line-height: 19px;
        }

        .desc {
          font-size: 12px;
          color: #0cc85c;
          height: 19px;
          line-height: 19px;
        }
      }

      .nav {
        margin-top: 8px;

        p {
          flex: 1;
          height: 42px;
          line-height: 42px;
          text-align: center;
          color: #646464;

          span {
            display: block;
            width: max-content;
            margin: 0 auto;
            position: relative;
            font-size: 15px;
          }
        }

        .con {
          span {
            color: #0cc85c;

            &::after {
              content: "";
              position: absolute;
              display: block;
              bottom: 0;
              left: 0;
              right: 0;
              height: 3px;
              background-color: #0cc85c;
              border-radius: 3px;
            }
          }
        }
      }
    }

    .game-introduce {
      padding: 5px 12px 10px;
      background-color: #fff;

      .head {
        h4 {
          font-size: 15px;
          height: 40px;
          line-height: 40px;
          color: #3e3e3e;
        }
      }

      .tags {
        display: flex;
        overflow-y: hidden;
        overflow-x: scroll;

        &::-webkit-scrollbar {
          width: 0;
          height: 0;
        }

        &::-webkit-scrollbar-thumb {
          width: 0;
          height: 0;
        }

        p {
          color: #0cc85c;
          padding: 0 8px;
          border: 1px solid #dbdbdb;
          border-radius: 5px;
          height: 24px;
          line-height: 24px;
          box-sizing: border-box;
          width: max-content;
          flex-shrink: 0;
          font-size: 12px;
          margin-right: 10px;
        }

        span {
          color: #969696;
        }
      }

      .body {
        margin-top: 10px;
      }

      .banner {
        display: flex;
        overflow-y: hidden;
        overflow-x: scroll;
        height: 170px;

        .img {
          width: 300px;
          height: 170px;
          border-radius: 5px;
          overflow: hidden;
          margin-right: 6px;
          flex-shrink: 0;
        }
      }

      .text {
        line-height: 21px;
        color: #646464;
        font-size: 15px;
        margin-top: 14px;
        margin-bottom: 18px;
      }

      .foot {
        display: flex;
        justify-content: center;
      }

      .toggle {
        // background: url("../imgs/down-show.png") no-repeat 0 0;
        background-size: 14px 8px;
        width: 14px;
        height: 8px;
      }
    }

    .game-introduce .banner::-webkit-scrollbar,
    .game-introduce .banner::-webkit-scrollbar-thumb {
      width: 0;
      height: 0;
    }

    .update {
      .head {
        h4 {
          display: flex;
          align-items: center;
          justify-content: space-between;
        }

        p {
          height: 15px;
          line-height: 15px;
          color: #9a9a9a;
          font-size: 12px;
        }
      }

      .history {
        font-size: 12px;
        color: #707070;
      }

      .detailed {
        h4 {
          height: 31px;
          line-height: 31px;
          color: #323232;
          font-size: 15px;
          margin-bottom: 5px;
        }
      }

      .table {
        padding: 3px 12px 12px;
        background-color: #fafafa;
        border-radius: 5px;
        box-shadow: 0 0 5px 0 #eee;
      }

      .thead {
        color: #383838;
        font-size: 12px;
        height: 42px;
        line-height: 42px;
      }

      .tbody {
        display: flex;
        flex-wrap: wrap;

        p {
          width: calc(100% / 3);

          span {
            font-size: 12px;
            height: 23px;
            line-height: 23px;
            display: block;
          }

          .key {
            color: #969696;
            color: #646464;
          }

          em {
            font-style: normal;
            color: #0cc85c;
          }
        }
      }
    }

    .recommended {
      margin-top: 10px;
      padding: 0 12px;
      background-color: #fff;

      .head {
        h4 {
          font-size: 15px;
          color: #323232;
          height: 50px;
          line-height: 50px;
        }
      }

      .body {
        display: flex;
        // justify-content: space-between;
        flex-wrap: wrap;

        .game-item {
          flex-shrink: 0;
          width: 80px;
          margin-bottom: 6px;
          margin-right: calc((100% - 320px) / 3);

          &:nth-child(4n) {
            margin-right: 0;
          }

          &:active {
            box-shadow: 0 0 80px 0 inset rgba($color: #000000, $alpha: 0.2);
            opacity: 0.9;
          }

          .icon {
            border-radius: 8px;
            overflow: hidden;
          }

          .text {
            height: 31px;
            line-height: 31px;
            font-size: 14px;
            color: #646464;

            span {
              display: block;
            }
          }
        }
      }
    }

    .ipone-card {
      p {
        margin-top: 10px;
        background-color: #fff;
        height: 46px;
        line-height: 46px;
        box-sizing: border-box;
        border-bottom: 1px solid #dddddd;
        color: #0cc85c;
        text-align: center;
        font-size: 13px;

        ::before {
          content: "";
          display: inline-block;
          //   background: url("../imgs/iphone-card.png") no-repeat 0 0;
          background-size: 18px 13px;
          width: 12px;
          height: 13px;
          margin-right: 5px;
        }

        span {
          color: #969696;
        }
      }
    }

    .download {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 45px;
      position: sticky;
      bottom: 0;
      background-color: #fff;

      a {
        display: block;
        height: 30px;
        line-height: 30px;
        width: 188px;
        text-align: center;
        border-radius: 15px;
        background-color: #19d469;
        font-size: 15px;
        color: #fff;
      }
    }

    
  }
}
</style>